import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class changefilename extends JFrame {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField text_path;;
	private JScrollPane scrollPane;
	private static JTextArea textArea;
	private List<File> list;  //存放排序后的文件
	private String newString = "";//新字符串
	private int index=1;   //当前文件名的前缀
	private JTextField text_index;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					changefilename frame = new changefilename();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public changefilename() {
		setResizable(false);
		setTitle("按修改时间递增顺序修改文件名");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 588, 537);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("文件路径：");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		lblNewLabel.setBounds(105, 40, 68, 29);
		contentPane.add(lblNewLabel);

		text_path = new JTextField();
		text_path.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		text_path.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER) {
					run();
				}
			}
		});
		text_path.setBounds(190, 37, 282, 32);
		contentPane.add(text_path);
		text_path.setColumns(10);

		JButton btnNewButton = new JButton("开始修改");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				run();
			}
		});
		btnNewButton.setBounds(174, 128, 104, 32);
		contentPane.add(btnNewButton);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(60, 170, 467, 308);
		contentPane.add(scrollPane);

		textArea = new JTextArea();
		textArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		scrollPane.setViewportView(textArea);

		JButton btnNewButton_1 = new JButton("清空路径");
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				text_path.setText("");
			}
		});
		btnNewButton_1.setBounds(313, 128, 104, 32);
		contentPane.add(btnNewButton_1);

		text_index = new JTextField();
		text_index.setText("1");
		text_index.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		text_index.setColumns(10);
		text_index.setBounds(190, 79, 104, 32);
		contentPane.add(text_index);

		JLabel lblNewLabel_1 = new JLabel("开始序号：");
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		lblNewLabel_1.setBounds(105, 82, 68, 29);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("默认为1  仅填入数字");
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel_2.setBounds(313, 87, 130, 15);
		contentPane.add(lblNewLabel_2);

		changefilename.this.setLocationRelativeTo(null);
	}
	public void run(){
		String m=text_index.getText().trim();
		try {
			int n=Integer.parseInt(m);
			if(n>0) {
				index=n;
			}else {
				JOptionPane.showMessageDialog(contentPane, "输入的数字不能小于1，请重新输入序号");
				return;
			}
			String path=text_path.getText().trim();
			textArea.setText("");
			list = getFileSort(path);
			recursiveTraversalFolder();
		}catch(NumberFormatException e){
			JOptionPane.showMessageDialog(contentPane, "输入有误，请重新输入序号");
		}
	}
	/**
	 * 获取目录下所有文件(按时间排序)
	 *
	 * @param path
	 * @return
	 */
	public List<File> getFileSort(String path) {
		List<File> list = getFiles(path, new ArrayList<File>());
		if (list != null && list.size() > 0) {
			Collections.sort(list, new Comparator<File>() {
				@Override
				public int compare(File file, File newFile) {
					if (file.lastModified() < newFile.lastModified()) {
						return -1;
					} else if (file.lastModified() == newFile.lastModified()) {
						return 0;
					} else {
						return 1;
					}
				}
			});
		}
		return list;
	}

	/**
	 *
	 * 获取目录下所有文件
	 *
	 * @param realpath
	 * @param files
	 * @return
	 */
	public List<File> getFiles(String realpath, List<File> files) {
		File realFile = new File(realpath);
		if (realFile.isDirectory()) {
			File[] subfiles = realFile.listFiles();
			for (File file : subfiles) {
				if (file.isDirectory()) {
					getFiles(file.getAbsolutePath(), files);
				} else {
					files.add(file);
				}
			}
		}
		return files;
	}

	/**
	 * 批量修改文件名
	 */
	public void recursiveTraversalFolder() {
		if (null == list || list.size() == 0) {
			JOptionPane.showMessageDialog(contentPane, "文件夹是空的!");
			return;
		} else {
			File newDir = null;// 文件所在文件夹路径+新文件名
			String newName = "";// 新文件名
			String fileName = null;// 旧文件名
			File parentPath = new File("");// 文件所在父级路径
			for (File file : list) {
				fileName = file.getName();
				String m=fileName.substring(fileName.indexOf("."));//后缀名  .jpg
				parentPath = file.getParentFile();
				if (index < 10) {
					newString = "00" + index + m;
				} else if (index < 100) {
					newString = "0" + index + m;
				} else {
					newString = "" + index + m;
				}
				newName = fileName.replaceAll(fileName, newString);// 新名字
				newDir = new File(parentPath + "/" + newName);// 文件所在文件夹路径+新文件名
				file.renameTo(newDir);// 重命名
				String str=textArea.getText().trim();
				if(index>1) {
					str+="\n";
				}
				str=str+"fileName:"+fileName+"\n"+"修改后：" + newDir+"\n";
				textArea.setText(str);
				index++;
			}
		}
	}
}
